package com.jhws.util;

import com.jhws.common.PropertiesReader;

import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class MailUtil {
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	public static final String HOST = PropertiesReader.getProperty("HOST");
	public static final String PROTOCOL = PropertiesReader.getProperty("PROTOCOL");
	public static final String PORT = PropertiesReader.getProperty("PORT");
	public static final String fromEmail = PropertiesReader.getProperty("fromEmail");
	public static final String username = PropertiesReader.getProperty("username");
	public static final String password = PropertiesReader.getProperty("password");
	
	public static boolean sendMail(String toEmail, String content) 
    {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties props = System.getProperties();
        props.put("mail.smtp.host", HOST); 
        props.put("mail.smtp.ssl.enable","true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFaputctory.fallback", "false"); 
        props.setProperty("mail.smtp.port", PORT); 
        props.setProperty("mail.smtp.socketFactory.port", PORT);
        props.put("mail.smtp.auth", "true");
        
        Multipart mPart = new MimeMultipart(); 
        BodyPart mBodyContent = new MimeBodyPart(); 

        Session session = Session.getInstance(props, new Authenticator(){ 
        	protected PasswordAuthentication getPasswordAuthentication() { 
        		return new PasswordAuthentication(username, password); 
        }}); 
        session.setDebug(true); 
        Message msg = new MimeMessage(session); 
        Transport trans = null;
        try 
        { 
            msg.setSubject("账号修改密码验证码"); 
            mBodyContent.setContent(content, "text/html;charset=UTF-8");
            mPart.addBodyPart(mBodyContent); 
            msg.setContent(mPart); 
            
            try
            { 
            	msg.setFrom(new InternetAddress(fromEmail,"居生活邮箱验证码"));
            } 
            catch (UnsupportedEncodingException e) 
            { 
            	e.printStackTrace();
            } 
            
            InternetAddress[] address = InternetAddress.parse(toEmail); 
            msg.setRecipients(Message.RecipientType.TO, address); 
            trans = session.getTransport(PROTOCOL); 
            trans.connect(HOST, username, password);
            Transport.send(msg); 
            trans.close();
        } 
        catch (MessagingException ex) 
        { 
            try 
            { 
                if (null != trans) 
                { 
                    trans.close(); 
                } 
            } 
            catch (Exception e) 
            { 
            	e.printStackTrace();
            } 
            ex.printStackTrace();
            return false; 
        } 
        return true; 
    }
}